﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Web.Code;
using DevFx.Biz;
using DevFx.Model;
using DevFx.FrameWork;
using DevFx.Utility;

namespace DevFx.Web.Pages.Rights
{
    public partial class ReportList : BasePage
    {
        #region 变量
        private vReportBiz vrbiz = new vReportBiz();
        private ReportBiz rbiz = new ReportBiz();
        private RoleBiz rolebiz = new RoleBiz();
        #endregion

        #region 属性

        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                BindRole();
                this.lblName.Text = "无";
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hfReportID.Value))
            {
                MessageBox.Show(this.Page,"请选择一个节点");
            }
            else if (this.hfReportID.Value == Guid.Empty.ToString())
            {
                var r = new SysReport();
                r.ReportID = Guid.NewGuid();
                r.RoleID = new Guid(this.ddlRole.Value);
                r.ParentReportID = Guid.Empty;
                r.ReportType = (int)ReportType.RoleReport;
                r.OrderNo = 0;
                r.Level = 1;
                r.Path = r.ParentReportID.Value.ToString() + "/";
                r.Disabled = false;
                r.CreateBy = UserID;
                r.ModifyBy = UserID;
                r.CreateDate = r.ModifyDate = DateTime.Now;
                rbiz.Create(r);
            }
            else
            {
                var r = new SysReport();
                var oldr = rbiz.GetByCondition(p => p.ReportID == new Guid(this.hfReportID.Value));
                r.ReportID = Guid.NewGuid();
                r.RoleID = new Guid(this.ddlRole.Value);
                r.ParentReportID = oldr.ReportID;
                r.ReportType = (int)ReportType.RoleReport;
                r.OrderNo = 0;
                r.Level = oldr.Level+1;
                r.Path =oldr.Path + "/";
                r.Disabled = false;
                r.CreateBy = UserID;
                r.ModifyBy = UserID;
                r.CreateDate = r.ModifyDate = DateTime.Now;
                rbiz.Create(r);
            }
            BindData();
        }
        protected void btndel_Click(object sender, EventArgs e)
        {
            BindData();
        }
        #endregion

        #region 方法
        private void BindData()
        {
            this.tvList.Nodes.Clear();
            var list = vrbiz.GetList(p => p.ReportType.Value == 2);
            TreeNode node = new TreeNode();
            node.Text = "角色汇报关系树";
            node.Value =Guid.Empty.ToString();
            GetNodeList(list, Guid.Empty, node);
            this.tvList.Nodes.Add(node);
            node.ExpandAll();
        }
        private void BindRole()
        {
            ddlRole.DataBind(rolebiz.GetList(p=>p.Disabled.Value==false),"RoleName","RoleID");
        }
        private void GetNodeList(List<vSysReport> List, Guid ParentID, TreeNode node)
        {
            List<vSysReport> list = List.FindAll(delegate(vSysReport m) { return m.ParentReportID == ParentID; });
            foreach (var b in list)
            {
                TreeNode NewNodes = new TreeNode();
                NewNodes.Text = b.RoleName;
                NewNodes.Value = b.ReportID.ToString();
                GetNodeList(List, b.ReportID, NewNodes);
                node.ChildNodes.Add(NewNodes);
            }
        }
        protected void tvList_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode Node = this.tvList.SelectedNode;
            this.lblName.Text = Node.Text;
            this.hfReportID.Value = Node.Value;
        }

        #endregion
       
    }
}